package com.input4hua.responsibility;

/**
 * 抽象处理类
 * @author jiahua.hu
 * @date 2019/3/30 
 */
public abstract class AbstractHandler implements Handler {
	private AbstractHandler nextHandler;

	@Override
	public final String handleMessage(RequestObj requestObj) {
		String responseResult;
		if (this.getLevel() == requestObj.getLevel()) {
			//当前节点自己处理
			responseResult = response(requestObj);
		}
		else {
			//当前节点无法处理
			if (this.nextHandler != null) {
				//交由下一节点处理
				responseResult = this.nextHandler.handleMessage(requestObj);
			}
			else {
				//处理流程结束，无下一节点可用于处理请求，请求结束
				responseResult = "处理失败，请知晓";
			}
		}
		return responseResult;
	}

	public void setNextHandler(AbstractHandler nextHandler) {
		this.nextHandler = nextHandler;
	}

	protected abstract int getLevel();

	protected  abstract String response(RequestObj requestObj);
}
